package cn.itheima.service.takedelivery.impl;

import cn.itheima.dao.takedelivery.PromotionRepository;
import cn.itheima.domain.Promotion;
import cn.itheima.page.PageBean;
import cn.itheima.service.takedelivery.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by millions on 2017/7/7.
 */
@Service
@Transactional
public class PromotionServiceImpl implements PromotionService {

    @Autowired
    private PromotionRepository promotionRepository;

    @Override
    public void save(Promotion model) {
        promotionRepository.save(model);
    }

    @Override
    public Page<Promotion> queryPage(Pageable pageable) {

        return promotionRepository.findAll(pageable);
    }

    @Override
    public PageBean<Promotion> queryPage(int rows, int page) {
        Pageable pageable=new PageRequest(page-1,rows);

        Page<Promotion> promotionPage = promotionRepository.findAll(pageable);

        PageBean<Promotion> pageBean=new PageBean<Promotion>();
        pageBean.setPageData(promotionPage.getContent());
        pageBean.setTotalCount(promotionPage.getTotalElements());
        return pageBean;
    }

    @Override
    public Promotion findPromotionById(Integer id) {

        return promotionRepository.findOne(id);
    }

    @Override
    public void updateStatus(Date date) {

         promotionRepository.updateStatus(date);
    }
}
